package com.cloudbroker.bcs.common.util;

import java.net.SocketTimeoutException;
import java.util.Map;

import org.apache.http.conn.ConnectTimeoutException;

import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bcs.common.constants.BasicErrorCodes;
import com.cloudbroker.bcs.common.http.HttpClient;

public class HSBCSHttpClient extends HttpClient {
    
    protected String remoteUrlBasePropName;
    
    protected String remoteUrlBase;
    
    protected boolean remoteUrlBasePropNameSet;
    
    public Map<String, Object> postFuncJSONFetchJSON(String functionId, Object param) {
        String url = makeFullRemoteUrl(functionId);
        Map<String, Object> result = null;
        try {
            result = super.postJSONFetchJSON(url, param, functionId);
        } catch (ConnectTimeoutException e) {
            result = VOUtil.genErrorResult(BasicErrorCodes.ACCESS_TIMEOUT);
        } catch (SocketTimeoutException e) {
            result = VOUtil.genErrorResult(BasicErrorCodes.PROCESSING_TIMEOUT);
        }
        if (null == result) {
            result = VOUtil.genCommonErrorResult();
        }
        return result;
    }
    
    public Map<String, Object> postFuncFormFetchJSON(String functionId, Map<String, Object> param, String charset) {
        String url = makeFullRemoteUrl(functionId);
        Map<String, Object> result = null;
        try {
            if (null == charset) {
                charset = IOUtil.CHARSET_NAME_UTF8;
            }
            result = super.postFormFetchJSON(url, param, functionId, charset);
        } catch (ConnectTimeoutException e) {
            result = VOUtil.genErrorResult(BasicErrorCodes.ACCESS_TIMEOUT);
        } catch (SocketTimeoutException e) {
            result = VOUtil.genErrorResult(BasicErrorCodes.PROCESSING_TIMEOUT);
        }
        if (null == result) {
            result = VOUtil.genCommonErrorResult();
        }
        return result;
    }
    
    public Map<String, Object> postFuncJSONFetchJSON(String functionId) {
        return postFuncJSONFetchJSON(functionId, null);
    }
    
    public Map<String, Object> postFuncFormFetchJSON(String functionId) {
        return postFuncFormFetchJSON(functionId, null, null);
    }
    
    protected String makeFullRemoteUrl(String url) {
        if (null != getRemoteUrlBase()) {
            return remoteUrlBase + url;
        }
        return url;
    }
    
    public String getRemoteUrlBase() {
        if (remoteUrlBasePropNameSet) {
            setRemoteUrlBase(ConfigUtil.get(remoteUrlBasePropName));
        }
        return remoteUrlBase;
    }
    
    public void setRemoteUrlBase(String remoteUrlBase) {
        if (null != remoteUrlBase && !remoteUrlBase.endsWith("/")) {
            remoteUrlBase += "/";
        }
        this.remoteUrlBase = remoteUrlBase;
    }
    
    public String getRemoteUrlBasePropName() {
        return remoteUrlBasePropName;
    }
    
    public void setRemoteUrlBasePropName(String remoteUrlBasePropName) {
        this.remoteUrlBasePropName = remoteUrlBasePropName;
        remoteUrlBasePropNameSet = true;
    }
    
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("HSBCSHttpClient [remoteUrlBase=").append(remoteUrlBase).append(", socketTimeout=")
                .append(socketTimeout).append(", connectTimeout=").append(connectTimeout).append(", pointLocal=")
                .append(pointLocal).append(", pointRemote=").append(pointRemote).append("]");
        return builder.toString();
    }
    
}
